package com.ikangtai.shecare.curve.mpchart;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.record.bean.UserRecordData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BBTMarkView extends MarkerView{

    private Context context;
    private LinearLayout markview;
    private TextView period_tv;
    private TextView bbt_tv;
    private ImageView heart_iv;
    private Map<Float, Boolean> xAxisValueSexMap = new HashMap<>();
    private List<UserRecordData> userRecordDataList;
    public static float selectedXAxisValue = 0;

    public BBTMarkView(final Context context, int layoutResource) {
        super(context, layoutResource);

        this.context = context;
        markview = (LinearLayout) findViewById(R.id.markview);
        markview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppLog.i("BBTMarkView onClick, e.getX() = " + selectedXAxisValue);
                Toast.makeText(context, "BBTMarkView e.getX() = " + selectedXAxisValue, Toast.LENGTH_LONG).show();
            }
        });

        // this markerview only displays a textview
        period_tv = (TextView) findViewById(R.id.period_tv);
        bbt_tv = (TextView) findViewById(R.id.bbt_tv);
        heart_iv = (ImageView) findViewById(R.id.heart);

        initXAxisValueSexMap();
    }

    private void initXAxisValueSexMap(){
        xAxisValueSexMap.clear();

        userRecordDataList = getUserRecordDataList();
        float xAxisValue = 0;
        boolean hasSex = false;
        int userRecordDataListLen = userRecordDataList.size();

        for(int i = 0; i < userRecordDataListLen; i++){
            UserRecordData userRecordData = userRecordDataList.get(i);

            xAxisValue = BBTLineUtil.getXAxisValue(userRecordData.getRecordDate());
            hasSex = userRecordData.isCopulationHaveSex();

            xAxisValueSexMap.put(xAxisValue, hasSex);
        }
    }

    private List<UserRecordData> getUserRecordDataList(){
        long endTime = System.currentTimeMillis() / 1000;
        long startTime = (long) (endTime - (BBTLineChart.XAXIS_TODAY - BBTLineChart.MIN_XAXIS_DAY) * 24 * 3600);
        startTime = startTime - startTime % (24 * 3600);

        return UserRecordData.getUserRecordDataList(context, App.userName, startTime, endTime);
    }

    @Override
    public int getXOffset(float xpos) {
        // this will center the marker-view horizontally
        return -(getWidth()/2) + 10;
    }

    @Override
    public int getYOffset(float ypos) {
        // this will cause the marker-view to be above the selected value
        return - (getHeight() + 20);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        AppLog.i("BBTMarkView e.getX() = " + e.getX());
        selectedXAxisValue = e.getX();
        if(BBTLineChart.bbtLineChartxValue2yValue.get(selectedXAxisValue) == null) {
            markview.setVisibility(View.GONE);
            return;
        }else
            markview.setVisibility(View.VISIBLE);

        int period_num = Menses.getPeriodNum(e.getX());

        if(period_num == 0)
            period_tv.setVisibility(View.GONE);
        else{
            period_tv.setVisibility(View.VISIBLE);
            period_tv.setText("周期第" + period_num + "天");
        }

        bbt_tv.setText(e.getY() + "°C"); // set the entry-value as the display text

        boolean heart = xAxisValueSexMap.get(e.getX());
        if(heart)
            heart_iv.setVisibility(View.VISIBLE);
        else
            heart_iv.setVisibility(View.GONE);
    }
}
